const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');

const app = express();

// 迎入users.js
const users = require('./routes/api/users');
const profiles = require('./routes/api/profiles');

// DB config
const db = require('./config/keys').mongoURL;

// 使用body-parser中间件
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json());

// Connect to mogodb
mongoose.connect(db)
        .then(() => {
          console.log("MongoDB Connected")
        })
        .catch(err => console.log(err));

// passport 初始化
app.use(passport.initialize());
// 这样写的目的，是为了方便分离代码
require("./config/passport")(passport);

// app.get("/", (req, res)　=> {
//   res.send("Hello World!")
// })

// 使用routes
app.use("/api/users",users);
app.use("/api/profiles", profiles);

const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`server running on port ${port}`)
})